﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Month
{
    public class AccountMonthTradeProduct
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public int ProductId { get; set; }
        /// <summary>
        /// 交易品种
        /// </summary>
        public virtual Product Product { get; set; }
        /// <summary>
        /// 多空方向
        /// </summary>
        public int Direction { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }




        /// <summary>
        /// 删除当月所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int month, int year, DB.DBManager db)
        {
            var query = from t in db.AccountMonthTradeProduct where t.Month == month && t.Year == year select t;
            foreach (Data.Month.AccountMonthTradeProduct c in query)
            {
                db.AccountMonthTradeProduct.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号月变化情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitMonthAccountProductChange(int month, int year, DB.DBManager db)
        {
            var query = db.Accounts.ToList();
            foreach (Sys.Account account in query)
            {
                #region i为0是多，i为1是空
                for (int i = 0; i < 2; i++)
                {
                    var _query = (from t in db.AccountDayTradeProduct where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i select t).ToList();
                    foreach (Day.AccountDayTradeProduct instru in _query)
                    {
                        var query2 = (from t in db.AccountMonthTradeProduct where t.Month == instru.TradeHistory.MPdate && t.Year == instru.TradeHistory.YPdate && t.AccountId == account.Id && t.Direction == i && t.ProductId == instru.ProductId select t).ToList();
                        var query3 = (from t in db.AccountDayTradeProduct where t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year && t.AccountId == account.Id && t.Direction == i && t.ProductId == instru.ProductId select t).ToList();
                        AccountMonthTradeProduct change = new AccountMonthTradeProduct();
                        change.AccountId = account.Id;
                        change.Direction = i;
                        change.ProductId = instru.ProductId;
                        if (query2.Count() > 0)//有记录更新
                        {
                            change = query2.First();
                        }
                        if (_query.Count() > 0)
                        {
                            change.BillCount = query3.Sum(a => a.BillCount);
                            change.DayCount = query3.Sum(a => a.DayCount);
                            change.Free = query3.Sum(a => a.Free);
                            change.LoseCount = query3.Sum(a => a.LoseCount);
                            change.Profit = query3.Sum(a => a.Profit);
                            change.SumLost = query3.Sum(a => a.SumLost);
                            change.SumWin = query3.Sum(a => a.SumWin);
                            change.UsedMargin = query3.Sum(a => a.UsedMargin);
                            change.WinCount = query3.Sum(a => a.WinCount);
                        }
                        change.Year = year;
                        change.Month = month;
                        if (query2.Count() == 0)
                        {
                            db.AccountMonthTradeProduct.Add(change);
                        }
                    }

                }
                #endregion
            }
            db.SaveChanges();
        }
    }
}